#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "libradarssd.h"

unsigned long elapsed(struct timeval* begin, struct timeval* end){
    long seconds = end->tv_sec - begin->tv_sec;
    long microseconds = end->tv_usec - begin->tv_usec;
    return seconds*1e6 + microseconds;
}

int main(int argc, char *argv[]){
    if(argc <= 1){
        printf("argment error, write time not provided!\n");
        return 0;
    }

    u64 test_time = 0;
    sscanf(argv[1],"%lu",&test_time);
    printf("Write %lu times\n",test_time);

    struct timeval begin, end;
    open_unvme();
    gettimeofday(&begin, 0);

    //u64 test_time = 1*44738560;
    //u64 test_time = 1*1000*1000; //for ai speed test
    u64 write_signal = 0;

    for(int i=0;i<test_time;i++){
        push_signal_noopen(i*100,i+1,i+2);
        write_signal++;
    }

    gettimeofday(&end, 0);
    close_unvme();
    printf("total elapsed %lu us(%f s)\n",elapsed(&begin, &end),elapsed(&begin, &end)/1000000.0);

    float MB = write_signal*FEATURE_SIZE*FEATURE_COUNT / (float)(1024*1024);
    float bandwith = MB/((double)elapsed(&begin, &end)/1000000);
    double average_latency = (double)elapsed(&begin, &end)/write_signal;
    printf("write %lu signals in %.2f MB\nBandwith:%.2f MB/s\nAverage latency:%.4lf us/signal\n",write_signal,MB,bandwith,average_latency);

    return 0;
}
